<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function afterTime()
{
    return date('Y-m-d H:i:s',time());
}
/**
 * 系统加密方法
 * @param string $data   要加密的字符串
 * @param string $key    加密密钥
 * @param int    $expire 过期时间 单位 秒
 * @return string
 */
function think_encrypt ($data, $key = '', $expire = 0)
{
    $key = md5(empty($key) ? \think\Config::get('DATA_AUTH_KEY') : $key);
    $data = base64_encode($data);
    $x = 0;
    $len = strlen($data);
    $l = strlen($key);
    $char = '';

    for ($i = 0; $i < $len; $i++) {
        if ($x == $l)
            $x = 0;
        $char .= substr($key, $x, 1);
        $x++;
    }

    $str = sprintf('%010d', $expire ? $expire + time() : 0);

    for ($i = 0; $i < $len; $i++) {
        $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
    }
    return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($str));
}

/**
 * 系统解密方法
 * @param  string $data 要解密的字符串 （必须是think_encrypt方法加密的字符串）
 * @param  string $key  加密密钥
 * @return string
 */
function think_decrypt ($data, $key = '')
{
    $key = md5(empty($key) ? \think\Config::get('DATA_AUTH_KEY') : $key);
    $data = str_replace(['-', '_'], ['+', '/'], $data);
    $mod4 = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    $data = base64_decode($data);
    $expire = substr($data, 0, 10);
    $data = substr($data, 10);

    if ($expire > 0 && $expire < time()) {
        return '';
    }
    $x = 0;
    $len = strlen($data);
    $l = strlen($key);
    $char = $str = '';

    for ($i = 0; $i < $len; $i++) {
        if ($x == $l)
            $x = 0;
        $char .= substr($key, $x, 1);
        $x++;
    }

    for ($i = 0; $i < $len; $i++) {
        if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        } else {
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return base64_decode($str);
}
/**
 * 获取状态码对应的详情
 * @param $code     状态码
 * @return array
 * @author Degree
 */
function get_code_info ($code,$msg='')
{

    $codeInfo = \think\Config::get('CODE_INFO');// 全局返回码
    if($code == 40901){
        $response = ['code' => 40901, 'msg' => $msg];
    }else{
        $response = isset($codeInfo[$code]) ? $codeInfo[$code] : ['code' => 40010, 'msg' => 'Error code without configuration'];
    }

    return $response;
}

/**
 * 获取客户端ip
 * @return mixed
 */
function get_client_ip()
{
    $ip = request()->ip();
    return $ip;
}

/**
 * 生成唯一订单号
 * @return string
 */
function order_num()
{
    return date('YmdHis',time()).rand(1000,9999);
}

/**
 * 根据时间戳返回天数小时
 * @param $time
 * @return string
 */
function wordTime($time) {
    $str = '';
    $hour = floor($time / 3600);

    if($hour < 24){
        $str = sprintf('%d小时前',$hour );
    }else{
        $day = floor($hour / 24 );
        $hour = $hour - ($day * 24);
        $str = sprintf('%d天%d小时前',$day,$hour );
    }
    return $str;
}